<template>
    <div class="app-container" :class="{ 'sidebar-open': isSidebarOpen }">
        <!-- Left Sidebar -->
        <aside class="left-sidebar">
            <div class="search-container">
                <input type="text" class="search-input" placeholder="搜索" v-model="searchQuery">
                <span class="search-shortcut">Ctrl + /</span>
            </div>

            <nav class="nav-menu">
                <div class="nav-item" v-for="item in navigation" :key="item.title">
                    <div class="nav-header" @click="toggleFolder(item)" :class="{ 'active': item.isOpen }">
                        <ChevronRight class="icon" :class="{ 'rotated': item.isOpen }" />
                        <span>{{ item.title }}</span>
                    </div>

                    <div class="nav-children" v-if="item.isOpen">
                        <router-link v-for="child in item.children" :key="child.path" :to="child.path" class="nav-link"
                            :class="{ 'active': currentPath === child.path }">
                            {{ child.title }}
                        </router-link>
                    </div>
                </div>
            </nav>
        </aside>

        <!-- Main Content -->
        <main class="main-content">
            <div class="content-wrapper">
                <MarkPreview :dailyLog="dailyLog" />
                <!-- <MarkEditor :dailyLog="dailyLog" /> -->
            </div>
        </main>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'
import { ChevronRight, Link, Settings, Menu } from 'lucide-vue-next'
import MarkEditor from '../utils/MarkEditor.vue'

const route = useRoute()
const searchQuery = ref('')
const isSidebarOpen = ref(false)

const navigation = ref([
    {
        title: 'Vue',
        isOpen: true,
        children: [
            // { title: 'Vue3简介', isOpen: true, path: '/vue3-intro' },  000000
            { title: 'Vue3简介', path: '/vue3-intro' },
            { title: '性能的提升', path: '/performance' },
            { title: '源码的升级', path: '/source-code' },
            { title: '拥抱TypeScript', path: '/typescript' }
        ]
    }
])

const tableOfContents = ref([
    { id: 'intro', title: '1. Vue3简介', level: 1 },
    { id: 'performance', title: '1.1 性能的提升', level: 2 },
    { id: 'source', title: '1.2 源码的升级', level: 2 },
    { id: 'typescript', title: '1.3 拥抱TypeScript', level: 2 }
])

const currentPath = computed(() => route.path)

const toggleFolder = (item) => {
    item.isOpen = !item.isOpen
}

const toggleSidebar = () => {
    isSidebarOpen.value = !isSidebarOpen.value
}
</script>

<style lang="scss">
.app-container {
    display: grid;
    grid-template-columns: 280px 1fr 280px;
    min-height: 100vh;
    background: #ffffff;
}

.left-sidebar {
    border-right: 1px solid #e5e7eb;
    padding: 1rem;
    background: #ffffff;

    .search-container {
        position: relative;
        margin-bottom: 1rem;

        .search-input {
            width: 100%;
            padding: 0.5rem 1rem;
            border: 1px solid #e5e7eb;
            border-radius: 6px;
            font-size: 0.875rem;

            &:focus {
                outline: none;
                border-color: #60a5fa;
            }
        }

        .search-shortcut {
            position: absolute;
            right: 0.75rem;
            top: 50%;
            transform: translateY(-50%);
            font-size: 0.75rem;
            color: #6b7280;
        }
    }
}

.nav-menu {
    .nav-item {
        margin-bottom: 0.5rem;
    }

    .nav-header {
        display: flex;
        align-items: center;
        padding: 0.5rem;
        cursor: pointer;
        border-radius: 6px;

        &:hover {
            background: #f3f4f6;
        }

        &.active {
            background: #f3f4f6;
        }

        .icon {
            width: 16px;
            height: 16px;
            margin-right: 0.5rem;
            transition: transform 0.2s;

            &.rotated {
                transform: rotate(90deg);
            }
        }
    }

    .nav-children {
        padding-left: 1.5rem;

        .nav-link {
            display: block;
            padding: 0.375rem 0.5rem;
            color: #374151;
            text-decoration: none;
            border-radius: 4px;

            &:hover {
                background: #f3f4f6;
            }

            &.active {
                color: #2563eb;
                background: #eff6ff;
            }
        }
    }
}

.main-content {
    padding: 2rem;
    // width: calc(100% - 280px - 280px);

    .content-wrapper {
        // max-width: 768px;
        margin: 0 auto;
    }
}

.right-sidebar {
    border-left: 1px solid #e5e7eb;
    padding: 1rem;

    .toc-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 1rem;
        padding-bottom: 0.5rem;
        border-bottom: 1px solid #e5e7eb;

        .toc-actions {
            display: flex;
            gap: 0.5rem;

            .icon {
                width: 16px;
                height: 16px;
                color: #6b7280;
                cursor: pointer;

                &:hover {
                    color: #374151;
                }
            }
        }
    }
}

.toc-nav {
    .toc-item {
        margin-bottom: 0.5rem;

        a {
            color: #374151;
            text-decoration: none;
            font-size: 0.875rem;

            &:hover {
                color: #2563eb;
            }
        }

        &.level-2 {
            padding-left: 1rem;
        }

        &.level-3 {
            padding-left: 2rem;
        }
    }
}

.mobile-toggle {
    display: none;
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    padding: 0.75rem;
    background: #2563eb;
    border: none;
    border-radius: 50%;
    color: white;
    cursor: pointer;
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);

    .icon {
        width: 20px;
        height: 20px;
    }
}

@media (max-width: 1024px) {
    .app-container {
        grid-template-columns: 1fr;
    }

    .left-sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 280px;
        transform: translateX(-100%);
        transition: transform 0.3s;
        z-index: 50;
    }

    .right-sidebar {
        display: none;
    }

    .mobile-toggle {
        display: block;
    }

    .sidebar-open {
        .left-sidebar {
            transform: translateX(0);
        }
    }
}
</style>